
using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;

namespace ComBrowser
{




	public partial class FlipperViewController : UIViewController
	{
		#region Constructors

		// The IntPtr and initWithCoder constructors are required for controllers that need 
		// to be able to be created from a xib rather than from managed code

		public FlipperViewController (IntPtr handle) : base(handle)
		{
			Initialize ();
		}

		[Export("initWithCoder:")]
		public FlipperViewController (NSCoder coder) : base(coder)
		{
			Initialize ();
		}

		public FlipperViewController () : base("FlipperViewController", null)
		{
			Initialize ();
		}

		MainViewController mainViewController;
		
		public FlipperViewController (MainViewController CtrlParent)
		{
			this.mainViewController = CtrlParent;
		}
		
		void Initialize ()
		{
		}

		#endregion


		NavBookViewController bookController;
		NavChapterViewController chapterController;
		int zeroBasedSelectedBookID = 0;

		public override void ViewDidLoad ()
		{
			base.ViewDidLoad ();
			bookController = new NavBookViewController (5, this);
			this.View.AddSubview (bookController.View);
			
			
		}

		public void SwitchToChapterView (int zeroBasedSelectedBookID)
		{
			
			chapterController = new NavChapterViewController (zeroBasedSelectedBookID, this);
			bookController.View.RemoveFromSuperview ();
			
			UIView.BeginAnimations (null);
			UIView.SetAnimationDuration (0.65);
			UIView.SetAnimationTransition (UIViewAnimationTransition.FlipFromRight, this.View, true);
			
			this.View.AddSubview (chapterController.View);
			this.View.SendSubviewToBack (bookController.View);
			
			UIView.CommitAnimations ();
		}

		public void SwitchBackToBookView (int zeroBasedSelectedBookID)
		{
			SwitchBackToBookView (zeroBasedSelectedBookID, true);
		}


		public void SwitchBackToBookView (int zeroBasedSelectedBookID, bool DoAnimation)
		{
			
			this.zeroBasedSelectedBookID = zeroBasedSelectedBookID;
			
			if (chapterController != null && chapterController.View != null) {
				chapterController.View.RemoveFromSuperview ();
				
				if (DoAnimation) {
					UIView.BeginAnimations (null);
					UIView.SetAnimationDuration (0.65);
					UIView.SetAnimationTransition (UIViewAnimationTransition.FlipFromRight, this.View, true);
				}
				
				this.View.AddSubview (bookController.View);
				this.View.SendSubviewToBack (chapterController.View);
				
				if (DoAnimation) {
					UIView.CommitAnimations ();
				}
			}
			chapterController = null;
		}

		public void CancelView ()
		{
			this.View.Hidden = true;
			SwitchBackToBookView (this.zeroBasedSelectedBookID, false);
		}

		public void ChapterPicked (int zeroBasedSelectedBookID, int zeroBasedChapterID)
		{
			this.mainViewController.ChapterPicked (zeroBasedSelectedBookID, zeroBasedChapterID);
			this.View.Hidden = true;
			SwitchBackToBookView (this.zeroBasedSelectedBookID, false);
		}
		
	}
}
